# -*- coding: utf-8 -*-
# @Time    : 2025/7/11 14:45
# @Author  : XinHuang
# @Email   : SA24231052@mail.ustc.edu.cn
# @File    : pycrab
# @Software: PyCharm
from model import FeedBackSystem
import numpy as np
import logging


def setup_logging(log_file='log.log'):
    """
    设置日志配置
    """
    logging.basicConfig(
        level=logging.INFO,
        # format="%(pathname)s:%(lineno)d\n%(asctime)s [%(levelname)s] %(message)s",
        format="%(asctime)s [%(levelname)s] %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
        encoding='utf-8',
        handlers=[
            logging.FileHandler(log_file),  # 将日志写入文件
            logging.StreamHandler()  # 同时输出到控制台
        ]
    )
    print('')  # 让控制台输出更好看
    logging.debug('已经设置好日志配置')


setup_logging()
logging.info('#running...')
# TODO: 394MHZ的数据
# omega0 = 2 * 394e6 * np.pi
# R = 18.4e9
# TODO: 197MHZ的数据
omega0 = 2 * 197e6 * np.pi
R = 17.3e9
logging.info(f"frea={omega0 / 2 / np.pi:.4e}, omega={omega0:.4e}, Rx={R:.4e}")
omega = omega0 + np.array((*np.linspace(-8e5, 0, 100000), *np.linspace(0, 8e5, 100000),)) * 2 * np.pi
# cavity = FeedBackSystem(R, omega0, 1.75e6, omega=omega, T_0=1.6e-06)  # 暂时拿HALF的回旋时间用着
cavity = FeedBackSystem(R, omega0, 1.75e6, omega=omega, T_0=1 / 78.2e3)  # 修改为论文中的数据
cavity.calculate_impedance()
# cavity.add_feedback(4400, 320e-9)
cavity.add_feedback(4400, 320e-9)
# cavity.add_OTFB(4400, 15 / 16, 29.22, 320e-9, 0, )
cavity.add_OTFB(10, 15 / 16, 29.5, 320e-9, 0, )
cavity.plot_results()

logging.info('#complete!\n')
